import './index.less';
import {Config} from '../../libs/';
export default React.createClass({
    getInitialState(){

        const self = this;
        return {
            open: false,
            num: 0,
            today: Moment(new Date()).format('YYYY年MM月DD日'),
            list: [
                [
                    {
                        "text": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "children": [{
                                    "type": "b",
                                    "key": null,
                                    "ref": null,
                                    "props": {"children": ["第", "一", "节"]},
                                    "_owner": null,
                                    "_store": {}
                                }, {
                                    "type": "br",
                                    "key": null,
                                    "ref": null,
                                    "props": {},
                                    "_owner": null,
                                    "_store": {}
                                }, ""]
                            },
                            "_owner": null,
                            "_store": {}
                        }, "type": "time"
                    },
                    {"text": "语d文", "id": "si00000000000002"},
                    {"text": "语文", "id": "si00000000000001"},
                    {"text": "语文", "id": "si00000000000001"},
                    {"text": "语文", "id": "si00000000000001"},
                    {"text": "语文", "id": "si00000000000001"},
                    {"text": "语文", "id": "si00000000000001"},
                    {"text": "语文", "id": "si00000000000001"}
                ],
                [
                    {
                        "text": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "children": [{
                                    "type": "b",
                                    "key": null,
                                    "ref": null,
                                    "props": {"children": ["第", "二", "节"]},
                                    "_owner": null,
                                    "_store": {}
                                }, {
                                    "type": "br",
                                    "key": null,
                                    "ref": null,
                                    "props": {},
                                    "_owner": null,
                                    "_store": {}
                                }, ""]
                            },
                            "_owner": null,
                            "_store": {}
                        }, "type": "time"
                    }, {"text": "数学", "id": "si00000000000002"}, {"text": "数学", "id": "si00000000000002"}, {
                    "text": "数学",
                    "id": "si00000000000002"
                }, {"text": "数学", "id": "si00000000000002"}, {"text": "数学", "id": "si00000000000002"}, {
                    "text": "数学",
                    "id": "si00000000000002"
                }, {"text": "数学", "id": "si00000000000002"}],
                [
                    {
                        "text": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "children": [{
                                    "type": "b",
                                    "key": null,
                                    "ref": null,
                                    "props": {"children": ["第", "三", "节"]},
                                    "_owner": null,
                                    "_store": {}
                                }, {
                                    "type": "br",
                                    "key": null,
                                    "ref": null,
                                    "props": {},
                                    "_owner": null,
                                    "_store": {}
                                }, ""]
                            },
                            "_owner": null,
                            "_store": {}
                        }, "type": "time"
                    }, {"text": "英语", "id": "si00000000000003"}, {"text": "数学", "id": "si00000000000002"}, {
                    "text": "英语",
                    "id": "si00000000000003"
                }, {"text": "英语", "id": "si00000000000003"}, {"text": "英语", "id": "si00000000000003"}, {
                    "text": "英语",
                    "id": "si00000000000003"
                }, {"text": "英语", "id": "si00000000000003"}],
                [
                    {
                        "text": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "children": [{
                                    "type": "b",
                                    "key": null,
                                    "ref": null,
                                    "props": {"children": ["第", "四", "节"]},
                                    "_owner": null,
                                    "_store": {}
                                }, {
                                    "type": "br",
                                    "key": null,
                                    "ref": null,
                                    "props": {},
                                    "_owner": null,
                                    "_store": {}
                                }, ""]
                            },
                            "_owner": null,
                            "_store": {}
                        }, "type": "time"
                    }, {"text": "物理", "id": "si00000000000004"}, {"text": "物理", "id": "si00000000000004"}, {
                    "text": "物理",
                    "id": "si00000000000004"
                }, {"text": "物理", "id": "si00000000000004"}, {"text": "物理", "id": "si00000000000004"}, {
                    "text": "物理",
                    "id": "si00000000000004"
                }, {"text": "物理", "id": "si00000000000004"}]
                , {"text": "午休", "type": "column"},
                [
                    {
                        "text": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "children": [{
                                    "type": "b",
                                    "key": null,
                                    "ref": null,
                                    "props": {"children": ["第", "五", "节"]},
                                    "_owner": null,
                                    "_store": {}
                                }, {
                                    "type": "br",
                                    "key": null,
                                    "ref": null,
                                    "props": {},
                                    "_owner": null,
                                    "_store": {}
                                }, ""]
                            },
                            "_owner": null,
                            "_store": {}
                        }, "type": "time"
                    }, {"text": "化学", "id": "si00000000000005"}, {"text": "化学", "id": "si00000000000005"}, {
                    "text": "化学",
                    "id": "si00000000000005"
                }, {"text": "化学", "id": "si00000000000005"}, {"text": "化学", "id": "si00000000000005"}, {
                    "text": "化学",
                    "id": "si00000000000005"
                }, {"text": "化学", "id": "si00000000000005"}],
                [
                    {
                        "text": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "children": [{
                                    "type": "b",
                                    "key": null,
                                    "ref": null,
                                    "props": {"children": ["第", "六", "节"]},
                                    "_owner": null,
                                    "_store": {}
                                }, {
                                    "type": "br",
                                    "key": null,
                                    "ref": null,
                                    "props": {},
                                    "_owner": null,
                                    "_store": {}
                                }, ""]
                            },
                            "_owner": null,
                            "_store": {}
                        }, "type": "time"
                    }, {"text": "生物", "id": "si00000000000006"}, {"text": "生物", "id": "si00000000000006"}, {
                    "text": "生物",
                    "id": "si00000000000006"
                }, {"text": "生物", "id": "si00000000000006"}, {"text": "生物", "id": "si00000000000006"}, {
                    "text": "生物",
                    "id": "si00000000000006"
                }, {"text": "生物", "id": "si00000000000006"}],
                [
                    {
                        "text": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "children": [{
                                    "type": "b",
                                    "key": null,
                                    "ref": null,
                                    "props": {"children": ["第", "七", "节"]},
                                    "_owner": null,
                                    "_store": {}
                                }, {
                                    "type": "br",
                                    "key": null,
                                    "ref": null,
                                    "props": {},
                                    "_owner": null,
                                    "_store": {}
                                }, ""]
                            },
                            "_owner": null,
                            "_store": {}
                        }, "type": "time"
                    }, {"text": "政治", "id": "si00000000000007"}, {"text": "政治", "id": "si00000000000007"}, {
                    "text": "政治",
                    "id": "si00000000000007"
                }, {"text": "政治", "id": "si00000000000007"}, {"text": "政治", "id": "si00000000000007"}, {
                    "text": "政治",
                    "id": "si00000000000007"
                }, {"text": "政治", "id": "si00000000000007"}],
                [
                    {
                        "text": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "children": [{
                                    "type": "b",
                                    "key": null,
                                    "ref": null,
                                    "props": {"children": ["第", "八", "节"]},
                                    "_owner": null,
                                    "_store": {}
                                }, {
                                    "type": "br",
                                    "key": null,
                                    "ref": null,
                                    "props": {},
                                    "_owner": null,
                                    "_store": {}
                                }, ""]
                            },
                            "_owner": null,
                            "_store": {}
                        }, "type": "time"
                    }, {"text": "思想品德", "id": "si00000000000008"}, {"text": "思想品德", "id": "si00000000000008"}, {
                    "text": "思想品德",
                    "id": "si00000000000008"
                }, {"text": "思想品德", "id": "si00000000000008"}, {"text": "思想品德", "id": "si00000000000008"}, {
                    "text": "思想品德",
                    "id": "si00000000000008"
                }, {"text": "思想品德", "id": "si00000000000008"}],
                [
                    {
                        "text": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "children": [{
                                    "type": "b",
                                    "key": null,
                                    "ref": null,
                                    "props": {"children": ["第", "久", "节"]},
                                    "_owner": null,
                                    "_store": {}
                                }, {
                                    "type": "br",
                                    "key": null,
                                    "ref": null,
                                    "props": {},
                                    "_owner": null,
                                    "_store": {}
                                }, ""]
                            },
                            "_owner": null,
                            "_store": {}
                        }, "type": "time"
                    }, {"text": "自习", "id": "si00000000000000"}, {"text": "自习", "id": "si00000000000000"}, {
                    "text": "自习",
                    "id": "si00000000000000"
                }, {"text": "自习", "id": "si00000000000000"}, {"text": "自习", "id": "si00000000000000"}, {
                    "text": "自习",
                    "id": "si00000000000000"
                }, {"text": "自习", "id": "si00000000000000"}],
                [
                    {
                        "text": {
                            "type": "div",
                            "key": null,
                            "ref": null,
                            "props": {
                                "children": [{
                                    "type": "b",
                                    "key": null,
                                    "ref": null,
                                    "props": {"children": ["第", "十", "节"]},
                                    "_owner": null,
                                    "_store": {}
                                }, {
                                    "type": "br",
                                    "key": null,
                                    "ref": null,
                                    "props": {},
                                    "_owner": null,
                                    "_store": {}
                                }, ""]
                            },
                            "_owner": null,
                            "_store": {}
                        }, "type": "time"
                    }, {"text": "自习", "id": "si00000000000000"}, {"text": "自习", "id": "si00000000000000"}, {
                    "text": "自习",
                    "id": "si00000000000000"
                }, {"text": "自习", "id": "si00000000000000"}, {"text": "自习", "id": "si00000000000000"}, {
                    "text": "自习",
                    "id": "si00000000000000"
                }, {"text": "自习", "id": "si00000000000000"}]],
            tips: {},
            selected: {}
        }
    },
    getDefaultProps(){
        return {
            multiSelect: true, //默认多选,false设置单选
            onchange: function (data) {

            }
        }
    },
    propTypes: {
        onchange: React.PropTypes.func,
    },

    componentWillReceiveProps(nextProps){
        if (!_.isEmpty(nextProps['classInfo'])) {
            const timetable = nextProps.classInfo.list['timetable'];
            var table = [];
            var Label = Config.CLASS_NUM;
            _.range(0, 10).forEach((item, index)=> {
                var class_num = [];
                if (index == 4) {
                    table.push({"text": "午休", "type": "column"})
                }
                timetable.forEach((item_, index_)=> {
                    var en_num = Label[index + 1].en;
                    class_num.push({
                        "text": item_[en_num + 'text'],
                        "id": item_[en_num]
                    });
                });
                table.push(class_num);
            });
            this.setState({
                list: table
            });
            // this.props.onchange(nextProps['selected']);

        }
    },
    getHeader(){
        const OneWeek = 7 * 1000 * 24 * 60 * 60;
        var week = Moment(Date.now() - this.state.num * OneWeek).isoWeekday(1);
        return _.range(0, 8).map((item, index)=> {
            if (!index) {
                return <th key={index}>{Moment(week._i).format('YYYY-MM')}月</th>;
            }
            return <th key={index}>周{index }<br/>
                <small>{Moment(week._d.getTime() + (index - 1) * (OneWeek / 7)).format('MM-DD')}</small>
            </th>;
        });
    },
    render(){
        const self = this;
        const OneWeek = 7 * 1000 * 24 * 60 * 60;
        var week = Moment(Date.now() - this.state.num * OneWeek).isoWeekday(1);
        var table = [];
        if (this.props.classInfo && !_.isEmpty(this.props.classInfo)) {
            const timetable = this.props.classInfo.list['timetable'];
            var Label = Config.CLASS_NUM;
            _.range(0, 10).forEach((item, index)=> {
                var class_num = [];
                if (index == 4) {
                    table.push({"text": "午休", "type": "column"})
                }
                timetable.forEach((item_, index_)=> {
                    var en_num = Label[index + 1].en;
                    class_num.push({
                        "text": item_[en_num + 'text'],
                        "id": item_[en_num]
                    });
                });
                table.push(class_num);
            });
        }
        console.log(Moment(new Date()).format('YYYY年MM月DD日'))
        return <div>
            <div className="row" style={{marginBottom: '10px'}}>
                <div className="col-sm-3">
                    <button disabled={self.state.num >= 0 ? 'disabled' : ''} className="btn btn-default btn-xs"
                            onClick={()=> {
                                self.setState({
                                    num: self.state.num + 1
                                })
                            }}
                            type="button">上一周
                    </button>
                </div>
                <div className="col-sm-6 text-center"> 今天：{self.state.today}</div>
                <div className="col-sm-3 text-right">
                    <button className="btn btn-default btn-xs" type="button"
                            onClick={()=> {
                                self.setState({
                                    num: self.state.num - 1
                                })
                            }}>下一周
                    </button>
                </div>
            </div>
            <div className="time-course">
                <table className="table table-bordered table-condensed">
                    <thead>
                    <tr>
                        {
                            _.range(0, 8).map((item, index)=> {
                                if (!index) {
                                    return <th key={index}>{Moment(week._i).format('YYYY-MM')}月</th>;
                                }
                                return <th key={index}>周{index }<br/>
                                    <small>{Moment(week._d.getTime() + (index - 1) * (OneWeek / 7)).format('MM-DD')}</small>
                                </th>;
                            })
                        }
                    </tr>
                    </thead>
                    <tbody>
                    {
                        table.map((item, idx)=> {
                            if (item.type == "column") {
                                return <tr key={idx}>
                                    <td colSpan={self.state.list[idx - 1].length}>{item.text}</td>
                                </tr>
                            }
                            return <tr key={idx}>
                                <td className="text-center">
                                    <b>第{idx + 1}节</b>
                                </td>
                                {item.map((item_lesson, xid)=> {
                                    if (item_lesson.type == 'time')return '';
                                    const OneWeek = 7 * 1000 * 24 * 60 * 60;
                                    var week = Moment(Date.now() - self.state.num * OneWeek).isoWeekday(1);
                                    var tm = week._d.getTime() + (xid) * (OneWeek / 7);
                                    let t =
                                        `${Moment(new Date(tm)).format('YYYY-MM-DD')}:${idx + 1}`;
                                    const selected = self.props.selected || {};
                                    return <td className={selected[t] ? 'select' : ''}
                                               onClick={()=> {
                                                   let t =
                                                       `${Moment(week._d.getTime() +
                                                           (xid - 1) * (OneWeek / 7)).format('YYYY-MM-DD')}:${idx + 1}`;
                                                   self.setState({
                                                           selected: self.props.multiSelect ? Object.assign({},
                                                               self.state.selected,
                                                               {
                                                                   [t]: self.state.selected[t] ? false : true
                                                               }) : {[t]: self.state.selected[t] ? false : true},

                                                       }
                                                       , item=> {
                                                           self.props.onchange(self.state.selected);
                                                       });
                                               }}
                                               key={xid}>
                                        <a href="javascript:void(0);">{item_lesson.text}</a>
                                    </td>
                                })}
                            </tr>
                        })
                    }
                    </tbody>
                </table>
            </div>
        </div>
    }
});